package org.cvcsc.weproject.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

import org.cvcsc.weproject.application.LoginApplicationService;
import org.cvcsc.weproject.controller.dto.LoginRequest;
import org.cvcsc.weproject.controller.dto.LoginResponse;
import org.cvcsc.weproject.controller.dto.RequestDtoImportStudentsForClazz;
import org.cvcsc.weproject.controller.dto.ResponseDtoStudent;
import org.cvcsc.weproject.domain.Clazz;
import org.cvcsc.weproject.repository.Finder;
import org.cvcsc.weproject.repository.ModelFinder;
import org.cvcsc.weproject.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/students")
public class StudentController {
        @Autowired
        private StudentService studentService;
        @Autowired
        private Finder<Clazz, Long> clazzRepository;
        @Autowired
        private ModelFinder modelFinder;
        @Autowired
        private LoginApplicationService loginApplicationService;


        @GetMapping("/clazz/{clazzId}")
        @Operation(summary = "获取指定班级的所有学生信息", description = "根据班级ID返回该班级的所有学生列表")
        @ApiResponses(value = {
                        @ApiResponse(responseCode = "200", description = "成功获取学生信息"),
                        @ApiResponse(responseCode = "404", description = "未找到对应班级的学生信息")
        })
        public List<ResponseDtoStudent> getStudentsByClazzId(
                         @PathVariable Long clazzId) {
                return modelFinder.findStudentsByClazzId(clazzId).stream()
                                .map(ResponseDtoStudent::fromEntity) // 使用 StudentDTO 的静态工厂方法进行转换
                                .collect(Collectors.toList());
        }

        @PostMapping("/clazz/{clazzId}")
        @Operation(summary = "更新指定班级的学生信息", description = "为指定班级更新或创建学生信息")
        @ApiResponses(value = {
                        @ApiResponse(responseCode = "200", description = "成功更新学生信息"),
                        @ApiResponse(responseCode = "400", description = "请求参数有误，无法更新学生信息")
        })
        public void importStudentsForClazz(
                        @Parameter(description = "班级ID", required = true) @PathVariable Long clazzId,
                        @RequestBody RequestDtoImportStudentsForClazz requestDto) {
                Clazz clazz = this.clazzRepository.findById(clazzId)
                                .orElseThrow(() -> new RuntimeException("班级不存在"));
                studentService.importStudentsForClazz(clazz, requestDto.getListOfStudentNameAndNumber());
        }

        @PostMapping("/login")
        @Operation(summary = "学生登录", description = "学生使用学号和密码登录系统")
        @ApiResponses(value = {
                        @ApiResponse(responseCode = "200", description = "登录成功"),
                        @ApiResponse(responseCode = "401", description = "认证失败")
        })
        public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
                try {
                        LoginResponse response = loginApplicationService.login(request);
                        return ResponseEntity.ok(response);
                        //return ResponseEntity.ok(new LoginResponse(1L, "test", false, 1L, "token"));
                } catch (AuthenticationException e) {
                        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
                }
        }

        @DeleteMapping("/clazz/{clazzId}")
        @Operation(summary = "删除指定班级的所有学生信息", description = "根据班级ID删除该班级的所有学生信息")
        @ApiResponses(value = {
                        @ApiResponse(responseCode = "200", description = "成功删除学生信息"),
                        @ApiResponse(responseCode = "500", description = "删除过程中出现错误")
        })
        public void deleteAllStudentsByClazzId(
                        @Parameter(description = "班级ID", required = true) @PathVariable Long clazzId) {
                studentService.deleteAllStudentsByClazzId(clazzId);
        }
}
